/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.workingtimemanager.urb.db;

import android.content.Context;
import android.widget.Toast;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.config.AndroidSupport;
import com.db4o.config.EmbeddedConfiguration;
import de.workingtimemanager.urb.entities.Project;
import de.workingtimemanager.urb.entities.User;
import de.workingtimemanager.urb.entities.WorkDay;
import java.util.Vector;

/**
 *
 * @author CK05102
 */
public class DatabaseHandler implements IDBHandler {

    private ObjectContainer database;

    public DatabaseHandler() {
    }

    public void open(String name, EmbeddedConfiguration conf) {
        database = Db4oEmbedded.openFile(conf, name);
    }

    public void open(String name) {
        EmbeddedConfiguration conf = Db4oEmbedded.newConfiguration();
        conf.common().add(new AndroidSupport());
        conf.common().objectClass(User.class).cascadeOnDelete(true);
        conf.common().objectClass(User.class).cascadeOnUpdate(true);
        conf.common().objectClass(Project.class).cascadeOnDelete(true);
        conf.common().objectClass(Project.class).cascadeOnUpdate(true);
        conf.common().objectClass(WorkDay.class).cascadeOnDelete(true);
        conf.common().objectClass(WorkDay.class).cascadeOnUpdate(true);
        database = Db4oEmbedded.openFile(conf, name);
    }

    @Override
    public void open(Context context) {
//        String filePath = context.getFilesDir() + "/android.db4o";
        String filePath = context.getExternalFilesDir(null) + "/android.db4o";        
//        Toast.makeText(context,filePath,3000).show();
        final EmbeddedConfiguration conf = Db4oEmbedded.newConfiguration();
        conf.common().add(new AndroidSupport());
        conf.common().objectClass(User.class).cascadeOnDelete(true);
        conf.common().objectClass(User.class).cascadeOnUpdate(true);
        conf.common().objectClass(Project.class).cascadeOnDelete(true);
        conf.common().objectClass(Project.class).cascadeOnUpdate(true);
        conf.common().objectClass(WorkDay.class).cascadeOnDelete(true);
        conf.common().objectClass(WorkDay.class).cascadeOnUpdate(true);
        database = Db4oEmbedded.openFile(conf, filePath);
    }

    @Override
    public void close() {
        database.close();
    }

    @Override
    public void insert(Object object) {
        database.store(object);
    }

    @Override
    public void delete(Object object) {
        database.delete(object);
    }

    @Override
    public void deleteAll() {
        ObjectSet<User> users = database.queryByExample(User.class);
        for (User u : users) {
            database.delete(u);
        }
    }

    @Override
    public User getUser(String name) {
        ObjectSet<User> os = database.queryByExample(new User(name, null));
        return (User) os.next();
    }

    @Override
    public Vector<User> getAllUsers() {
        ObjectSet<User> os = database.queryByExample(User.class);
        Vector<User> users = new Vector<User>();
        for (User u : os) {
            users.add(u);
        }
        return users;
    }

    @Override
    public User getActiveUser() {
        ObjectSet<User> os = database.queryByExample(User.class);
        for (User u : os) {
            if (u.isActive()) {
                return u;
            }
        }
        return null;
    }
}
